package com.gzw.dms.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.bean.CuotomerInfo;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/8/31.
 */
public class SelectCustomerAdapter extends BaseAdapter {
    private Context mContext;
    private List<CuotomerInfo> lsMap;
    private String type = "0";
    private static HashMap<Integer, Boolean> isSelected;

    public SelectCustomerAdapter(Context _mContext,
                               List<CuotomerInfo> _lsMap, String _type) {
        this.mContext = _mContext;
        this.lsMap = _lsMap;
        this.type = _type;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();

    }
    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lsMap.size(); i++) {
            getIsSelected().put(i, false);
        }
    }
    @SuppressLint("ViewHolder")
    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        // 加载布局文件
        final  Holder mHolder;
        if (type.equals("1")) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_selectdepat, null);
                mHolder = new Holder();

                mHolder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
                mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
                mHolder.cb.setVisibility(View.GONE);
                convertView.setTag(mHolder);
            } else {
                mHolder = (Holder) convertView.getTag();
            }

            mHolder.tv_text.setText(lsMap.get(arg0).getCustomer_name());
            mHolder.tv_text.setTag(lsMap.get(arg0).getCustomer_id());
        }else {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_selectdepat, null);
                mHolder = new Holder();

                mHolder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
                mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);

                convertView.setTag(mHolder);
            } else {
                mHolder = (Holder) convertView.getTag();
            }


            mHolder.tv_text.setText(lsMap.get(arg0).getCustomer_name());
            mHolder.tv_text.setTag(lsMap.get(arg0).getCustomer_id());

            mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mHolder.cb.setChecked(isChecked);
                    isSelected.put(arg0, isChecked);
                }
            });
            mHolder.cb.setChecked(getIsSelected().get(arg0));
        }
        return convertView;
    }
    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        SelectCustomerAdapter.isSelected = isSelected;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return lsMap.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lsMap.size();
    }
    public static class Holder {

        public TextView tv_text;
        public CheckBox cb;
    }
}